package controllers

import (
	//"fmt"
	"net/http"

	"github.com/gin-gonic/gin"

	"huaxrobot/services"
)

type PlanDataInfo struct {
	TestEnv          string       `json:"testenv"`
	TestProduct      string        `gorm:"column:planproduct"`
	TestModule       string         `json:"testmodule"`
}


type InformData struct {
	PlanID          string       `json:"planid"`
	CaseTotal     uint16        `json:"casetotal"`
	CasePass       uint16         `json:"casepass"`
	CaseFail       uint16         `json:"casefail"`
	CaseOmit       uint16         `json:"caseomit"`
}


func PlanAdd(c *gin.Context) {
	plandatainfo := PlanDataInfo{}	
	err := c.ShouldBindJSON(&plandatainfo)
	if err != nil {
		c.JSON(http.StatusBadRequest, "参数错误")
		return
	}
	testenv := plandatainfo.TestEnv
	testproduct := plandatainfo.TestProduct
	testmodule := plandatainfo.TestModule
	res := services.PlanAdd(testproduct, testenv, testmodule)
	c.JSON(http.StatusOK, res)
}




func PlanJenkinsAdd(c *gin.Context) {
	plandatainfo := services.PlanJenkinsPlan{}
	err := c.ShouldBindJSON(&plandatainfo)
	if err != nil {
		c.JSON(http.StatusBadRequest, "参数错误")
		return
	}
	res := services.PlanJenkinsAdd(plandatainfo)
	c.JSON(http.StatusOK, res)
}


func PlanJenkinsAddbyName(c *gin.Context) {
	projectname := c.Param("projectname")	
	if projectname == "" {
		c.JSON(http.StatusBadRequest, "参数错误")
		return
	}
	res := services.PlanJenkinsAddbyName(projectname)
	c.JSON(http.StatusOK, res)
}


func PlanGet(c *gin.Context) {	
	planid := c.Param("planid")
	plan := services.PlanGet(planid)
	c.JSON(http.StatusOK, plan)
}


func PlanGetList(c *gin.Context) {
	plans := services.PlanGetList()
	c.JSON(http.StatusOK, plans)
}

func PlanDelete(c *gin.Context) {
	planid := c.Param("planid")
	res := services.PlanDelete(planid)
	c.JSON(http.StatusOK, res)
}


func ToPlanResult(c *gin.Context) {
	informdata := InformData{}
	err := c.ShouldBindJSON(&informdata)
	if err != nil {
		c.JSON(http.StatusBadRequest, "参数错误")
		return
	}
	planid := informdata.PlanID
	casetotal := informdata.CaseTotal
	casepass := informdata.CasePass
	casefail := informdata.CaseFail
	caseomit := informdata.CaseOmit
	res := services.ToPlanResult(planid, casetotal , casepass, casefail, caseomit)
	c.JSON(http.StatusOK, res)
}